from datetime import datetime
import tkinter as tk
from DataRecorder import Recorder


def log_message(widget, message, color="black"):
    widget.config(state=tk.NORMAL)
    # 定义 tag
    tag_name = f"color_{color}"
    widget.tag_configure(tag_name, foreground=color)

    # 插入带颜色的文本（在当前插入点）
    widget.insert(tk.END, message + "\n", tag_name)
    # 只有当用户不在查看历史日志时才自动滚动到底部
    from tkinter import INSERT
    widget.see(tk.END)
    widget.config(state=tk.DISABLED)

def log_to_file(message):
    timestamp = datetime.now().strftime('%m.%d')
    r = Recorder(path=f'调价记录_{timestamp}.csv', cache_size=500)
    r.add_data(message)  # 接收dict数据
    r.record()

def clear_log_file():
    log_path = "bid_adjustment_log.txt"
    with open(log_path, "w", encoding="utf-8") as f:
        f.write("")